package com.farmpost.redisservice.config;

import cn.hutool.core.util.ArrayUtil;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: jinzewang
 * @desc: 集群redis
 */
@Component
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    private String password;

    private RedisProperties cluster;

    @Bean
    public RedissonClient getClient() {
        Config config = new Config();
        List<String> redisClusterAddressList = new ArrayList<>();
        RedisProperties.nodes.forEach(ip->
                redisClusterAddressList.add("redis://" + ip)
        );
        config.useClusterServers()
                .setScanInterval(20000)
                .addNodeAddress(ArrayUtil.toArray(redisClusterAddressList,String.class))
                .setPassword(password);
        return Redisson.create(config);
    }

}
